﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Ninject;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Wicresoft.EKA.ITSM.BusinessImpl;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class CIAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJPlanObject SMGXJPlanObject { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJPlanObject).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJPlanObject).Dispose();
            base.Dispose(disposing);
        }

        //Ticket保存时先把老的CI都标记删除了，然后再加新的，这里有排序的
        public object Post(string TicketID ,[FromBody] List<SMGXJPlanObjectModel> modelList)
        {
            try
            {
                Init();
                string result = string.Empty;
                SMGXJPlanObjectModel modele = new SMGXJPlanObjectModel();
                modele.cChr_MarkAsDelete = "Yes";
                result = SMGXJPlanObject.ModifyALL(modele, "'cChr_RelatedTicketID' = \"" + TicketID + "\"");
                //result = SMGXJPlanObject.Delete(TicketID);
                int orderid = 1;
                foreach (var model in modelList)
                {
                    model.cChr_OrderID = orderid;
                    model.cChr_RelatedTicketID = TicketID;
                    model.Submitter = UserName;
                    model.ShortDescription = "Create";
                    model.cChr_MarkAsDelete = "No";
                    orderid++;
                    SMGXJPlanObject.Create(model);
                }
                return "Success";
            }
            catch (Exception e)
            {
                return "Fail: " + e.Message;
            }
        }

        //获得CI列表
        public object GetObject(string cChr_RelatedTicketID)
        {
            Init();
            string Qual = "'cChr_RelatedTicketID'=\"" + cChr_RelatedTicketID + "\" AND 'cChr_MarkAsDelete' = \"No\"";
            return SMGXJPlanObject.GetList(Qual);
        }

        //废弃，逻辑由后台转至前台
        public object Put(string id ,[FromBody] SMGXJPlanObjectModel model)
        {
            Init();
            string result = string.Empty;
            model.ShortDescription = "Generate Rule list";
            return SMGXJPlanObject.Modify(model);
        }
    }
}
